<template>
  <div class="collect-page">
    <div class="context" v-if="!showCollect">
      <div class="box">
        <div class="top">
          <img src="../assets/images/c.png" alt="" />
        </div>
        <div class="bottom">
          <div class="login" @click="getCollect">立即登录</div>
          <span>登录即可查看/同步收藏记录</span>
        </div>
      </div>
    </div>
    <div class="collect" v-if="showCollect">
      <div class="top">
        <div :class="['left', {'active':tab=='1'}]" @click="change('1')">菜谱</div>
        <div :class="['right', {'active':tab=='2'}]" @click="change('2')">课程</div>
        <router-link tag="div" to="/search" class="search-icon">
          <img src="../assets/images/ic_search.png" alt="" />
        </router-link>
      </div>
      <div style="height:50px"></div>
      <div class="content" v-if="tab=='1'">
        <div class="ablum-lists">
          <div class="title">
            <div class="ttile-name" @click="show">
              <img src="../assets/images/向下箭头.png" alt="" v-if="!showAblum" />
              <img src="../assets/images/向上箭头.png" alt="" v-if="showAblum" />
              <span>菜谱专辑({{this.ablumlist.length}}个收藏专辑，0个创建专辑)</span>
            </div>
            <div class="all">查看全部</div>
          </div>
          <transition>
            <div class="collect-lists" v-if="showAblum">
              <div class="lists">
                <router-link tag="div" :to="'/novice?id='+item.id" class="collect-item" v-for="item in ablumlist" :key="item.id">
                  <div class="img">
                    <img :src="'https://pic.ecook.cn/web/'+ item.imageid+'.jpg!wl280'" alt="" />
                    <div class="num">{{item.recipeCount}}道菜谱</div>
                    <div class="mask"></div>
                  </div>
                  <div class="right">
                    <div class="txt-top">
                      <h4 class="line-ellipsis">{{item.name}}</h4>
                      <span>{{item.usernickname}}</span>
                    </div>
                    <div class="option-btm">
                      <div class="del" @click.stop="delAblum(item.id)">
                        <img src="../assets/images/lajitong.png" alt="" />
                        <span>删除</span>
                      </div>
                    </div>
                  </div>
                </router-link>
              </div>
            </div>
          </transition>
        </div>
        <div class="video-lists">
          <div class="title">
            <div class="ttile-name">
              <img src="../assets/images/向下箭头.png" alt="" />
              <span>视频菜谱</span>
            </div>
            <div class="all">查看全部</div>
          </div>
        </div>
        <div class="pic-lists">
          <div class="title">
            <div class="ttile-name" @click="showView">
              <img src="../assets/images/向下箭头.png" alt="" v-if="!showCom" />
              <img src="../assets/images/向上箭头.png" alt="" v-if="showCom" />
              <span>图文收藏</span>
            </div>
            <div class="all">查看全部</div>
          </div>
          <transition>
            <div class="collect-lists" v-if="showCom">
        <div class="lists">
          <router-link tag="div" :to="'/detail?id='+item.id" class="collect-item" v-for="item in lists" :key="item.id">
            <div class="img" v-if="item.authorname!='吴伟龙'">
              <img :src="'https://pic.ecook.cn/web/'+ item.imageid+'.jpg!wl280'" alt="" />
            </div>
            <div class="img" v-else>
              <img :src="'blob:http://localhost:8080/'+ item.imageid+''" alt="" />
            </div>
            <div class="right">
              <div class="txt-top">
                <h4 class="line-ellipsis">{{item.name}}</h4>
                <span>{{item.authorname}}</span>
              </div>
              <div class="option-btm">
                <div class="addto">
                  <img src="../assets/images/添加文件夹.png" alt="" />
                  <span>添加至</span>
                </div>
                <div class="del" @click.stop="del(item.id)">
                  <img src="../assets/images/lajitong.png" alt="" />
                  <span>删除</span>
                </div>
              </div>
            </div>
          </router-link>
        </div>
            </div>
          </transition>
        </div>
        </div>
     
        <div class="content" v-if="tab=='2'">
        <div class="ablum-lists">
          <div class="title">
            <div class="ttile-name">
              <img src="../assets/images/向下箭头.png" alt="" />
              <span>课程专辑(0)</span>
            </div>
            <div class="all">查看全部</div>
          </div>
        </div>
        <div class="video-lists">
          <div class="title">
            <div class="ttile-name">
              <img src="../assets/images/向下箭头.png" alt="" />
              <span>课程收藏(0)</span>
            </div>
            <div class="all">查看全部</div>
          </div>
        </div>
       
        </div>
    </div>
    <bottom-tab :index=3 ></bottom-tab>
  </div>
</template>

<script>
import BottomTab from "../components/BottomTab.vue";
import { Dialog } from 'vant'
export default {
  data() {
    return {
      showCollect: false,
      lists:[],//图文收藏
      ablumlist:[],//菜谱专辑
      showCom:false,
      showAblum:false,
      tempArr:[],
      tab:'1'
    };
  },
  methods: {
    getCollect() {
      let token = window.localStorage.getItem("token");
      if (!token) {
        this.$router.push("/login");
      }
    },
    //展示图文收藏
    showView(){
        if (this.lists.length!=0) {
          this.showCom=!this.showCom
          window.localStorage.setItem("flag",this.showCom)
        }
    },
    del(id){
        Dialog.confirm({
          message: '删除这个收藏吗?',
          width:'260px',
          confirmButtonColor:'blue',
          confirmButtonText:'删除',
          cancelButtonColor:'blue'
        })
        .then(() => {
          let index = this.lists.findIndex(item=>item.id==id)
          this.lists.splice(index,1)
          if (this.lists.length==0) {
            this.showCom = false
            window.localStorage.setItem("flag",false)
          }
          window.localStorage.setItem('collectList',JSON.stringify(this.lists))
        })
        .catch(() => {
          // on cancel
        });
        
    },
    //展示菜谱专辑
    show(){
      if (this.ablumlist.length!=0) {
        this.showAblum = !this.showAblum
        window.localStorage.setItem("flagAblum",this.showAblum)
      }
    },
    //删除
    delAblum(id){
      Dialog.confirm({
        message: '删除这个收藏吗?',
        width:'260px',
        confirmButtonColor:'blue',
        confirmButtonText:'删除',
        cancelButtonColor:'blue'
      })
      .then(() => {
        let index = this.ablumlist.findIndex(item=>item.id==id)
        this.ablumlist.splice(index,1)
        if (this.ablumlist.length==0) {
          this.showAblum = false
          window.localStorage.setItem("flagAblum",false)
        }
        window.localStorage.setItem('ablumCollect',JSON.stringify(this.ablumlist))
      })
      .catch(() => {
        // on cancel
      });
    },
    //课程、菜谱切换
    change(value){
      this.tab =value
    }
  },
  created() {
    let token = window.localStorage.getItem("token");
    let flag = window.localStorage.getItem("flag")
    let flagA = window.localStorage.getItem("flagAblum")
    //图文收藏
    let tempCollect = JSON.parse(window.localStorage.getItem("collectList"))
    this.lists = tempCollect==null?[]:tempCollect.reverse()
    if (flag=='true') {
      this.showCom = true
    }else if(flag=='false'){
      this.showCom = false
    }
    if (flagA=='true') {
      this.showAblum = true
    }else if(flagA=='false'){
      this.showAblum = false
    }

    //菜谱专辑
    let ablum = JSON.parse(window.localStorage.getItem("ablumCollect"))
    this.ablumlist = ablum==null?[]:ablum.reverse()

    //验证登录
    if (!token) {
      this.showCollect = false;
    } else {
      this.showCollect = true;
    }
    window.localStorage.setItem("tabId",3)
  },
  components: {
    BottomTab,
  },
};
</script>

<style lang="less" scoped>
.collect-page {
  width: 100%;
  height: 100%;
  overflow: hidden;

  .context {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    .box {
      height: 50%;
      .bottom {
        width: 100%;
        display: flex;
        flex-direction: column;
        text-align: center;
        align-items: center;

        .login {
          width: 100px;
          display: block;
          padding: 10px;
          border-radius: 15px;
          font-size: 14px;
          font-weight: 600;
          background-color: #fdc003;
          margin-bottom: 20px;
        }
        & > span {
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
  .collect {
    height: 100%;
    overflow-y: scroll;
    .top {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      background-color: #fff;

      .right,
      .left {
        height: 100%;
        width: 150px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 15px;
        color: #999;
        font-weight: bold;
        letter-spacing: 0.8px;
        position: relative;
      }
      .left{
        width: 145px;
        margin-left: 10px;
      }
      .search-icon {
        width: 18px;
        height: 18px;
        & > img {
          width: 18px;
          height: 18px;
        }
      }
      .active {
        color: #fdc003;

        &::after {
          content: "";
          width: 18px;
          height: 3px;
          border-radius: 4px;
          background-color: #fdc003;
          position: absolute;
          bottom: 4px;
          left: 0;
          right: 0;
          margin: 0 auto;
        }
      }
    }
    .content {
      width: 100%;
      margin-bottom: 65px;
      .pic-lists,
      .video-lists,
      .ablum-lists {
        width: 100%;
        background-color: #fff;
        box-sizing: border-box;
        margin-bottom: 15px;
        .title {
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 10px 15px;
          background-color: #f2f2f2;

          .ttile-name {
            display: flex;
            align-items: center;
            & > img {
              width: 15px;
              height: 15px;
              margin-right: 5px;
            }
            & > span {
              font-size: 13px;
              font-weight: bold;
            }
          }
          .all {
            font-size: 12px;
            color: #999;
          }
        }
      .collect-lists {
        width: 100%;
        // height: 405px;
        background-color: #fff;
        padding-top: 10px;
      .lists {
        .collect-item {
          padding: 5px 25px;
          height: 100px;
          display: flex;
          margin-bottom: 8px;

          .img {
            flex: 0 0 140px;
            height: 100px;
            border-radius: 8px;
            overflow: hidden;
            position: relative;
            & > img {
              width: 100%;
              height: 100%;
            }
            .num{
              position: absolute;
              top: 8px;
              right: 10px;
              color: #f2f2f2;
              font-weight: 700;
              font-size: 13px;
              z-index: 1;
            }
            .mask{
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              width: 100%;
              height: 40px;
              color: transparent;
              background-image: linear-gradient(rgb(88, 80, 80),transparent);
            }
            
          }
          .right {
            flex: 1;
            margin-left: 12px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .txt-top {
              & > h4 {
                font-size: 15px;
                font-weight: bold;
                margin: 5px 0 10px 0;
                letter-spacing: 0.8px;
                line-height: 22px;
              }
              & > span {
                color: #999;
                font-weight: bold;
                font-size: 13px;
                letter-spacing: 0.8px;
              }
            }
            .option-btm {
              display: flex;
              align-items: center;
              justify-content: flex-end;
              .addto {
                display: flex;
                align-items: center;
                & > img {
                  width: 18px;
                  height: 18px;
                  margin-right: 3px;
                }
                & > span {
                  font-size: 13px;
                  color: #999;
                  letter-spacing: 0.4px;
                }
              }
              .del {
                display: flex;
                align-items: center;
                margin-left: 15px;
                & > img {
                  width: 18px;
                  height: 18px;
                  margin-right: 3px;
                }
                & > span {
                  font-size: 13px;
                  color: #999;
                  letter-spacing: 0.4px;
                }
              }
            }
          }
        }
      }
    }
      }
    }
  }
}
    .v-enter {
        opacity: 0;
    }

    .v-leave-to {
        opacity: 0;
    }

    .v-enter-active,
    .v-leave-active {
        transition: all 0.5s ease-in-out;
    }
</style>